IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].GetInstitutionOrUnitOrDept') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].GetInstitutionOrUnitOrDept
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/******************************************************************              
* Name:   GetInstitutionOrUnitOrDept              
* Purpose:  Retrieves Institution or unit ordepartment(Not used at present as this feature is removed from custom reports)           
*              
* PARAMETERS(IN)              
* Name          Description                   
* -------------      -------------------------------------------              
* @institutionId     institution id (to fetch the corresponding departments,provided the institution is known)          
* @departmentId      department id (to fetch the corresponding units,provided the institution and departments are known)          
*-------------------
* 01-07-2008 added order by Name - SC # 2877002
*********************************************************************/           
CREATE procedure GetInstitutionOrUnitOrDept     
(@institutionId int=0,          
 @departmentId int=0          
)          
AS BEGIN          
SET NOCOUNT ON          
if @institutionId = 0 and @departmentId= 0          
 BEGIN          
    --brings back the list of Institutions          
  Select Id as [InstitutionId],[Name]      
  from fwkDomainOrganization where ParentOrganizationId =1 and id <> 1 order by Name          
 END          
else if @institutionId <> 0 and @departmentId =0          
 BEGIN          
  Select Id as [InstitutionId],[Name]       
 from fwkDomainOrganization where institutionId=@institutionId          
 END          
else if @institutionId <> 0 and @departmentId <>0          
 BEGIN          
  Select Id as [InstitutionId],[Name]     
 from fwkDomainOrganization           
   where institutionId=@institutionId           
   and departmentid = @departmentId                    
 END          
END 